#!/usr/bin/perl -w

use strict;
use Getopt::Long;



my $fichier_in;
my $out_comptage;
my $out_inconnu;
my %hash_fichier_in = () ;
my %hash_indice_debut = ();
my %hash_indice_fin = ();
my %hash_compteur = ();
my %hash_indice_delete = ();
my $pourcentage_app = 0;
my $pourcentage_test = 25;

GetOptions ('i=s' => \$fichier_in,
            'out1=s'   => \$out_comptage,
            'out2=s'	 => \$out_inconnu);

open (ARFF,'<',$fichier_in) || die "Can't open file:$!\n";



my $indice_ligne = 0;
my @tab_id;
my $flag_stop = 0;

while (<ARFF>) 
{
	my $l = $_;
	chomp($l);
	$l =~ s/^\s+|\s+$//g;

	$indice_ligne = $indice_ligne + 1;

	# sauvegarde de la ligne
	$hash_fichier_in{$indice_ligne} = $l;

	if($l =~ m/({)(.*)(}$)/)
	{
		my $les_ids = $2;
		@tab_id = split(/,/,$les_ids);
		# print "les_ids = $les_ids\n";
	}

	if($l =~ m/(^[^@])(.*)(,)(.*$)/)
	{
		my $id = $4;
		$hash_compteur{$id} += 1;
		$hash_indice_fin{$id} = $indice_ligne;
		if (!defined($hash_indice_debut{$id}))
		{
			$hash_indice_debut{$id} = $indice_ligne;
			# print "indice_ligne debut pour $id = $indice_ligne\n";
		}
	} 

}

my $i;

for ($i = 1; $i <= $indice_ligne; $i++)
{
	$hash_indice_delete{$i} = 0;
}	

foreach my $id (@tab_id)
{
	my $nb = $hash_compteur{$id};
	print "nb pour $id = $nb\n";
	my $nb_a_prendre = int(($pourcentage_test * $nb)/100 + 0.5);
	my $cpt = 0;
	my $borne_inf = $hash_indice_debut{$id};
	my $borne_sup = $hash_indice_fin{$id} + 1 ;
	while ($cpt < $nb_a_prendre) 
	{
		my $indice_a_prendre = int (rand($borne_sup-$borne_inf)) + $borne_inf;
		# print "indice_a_prendre = $indice_a_prendre\n";
		if($hash_indice_delete{$indice_a_prendre}==0)
		{
			$hash_indice_delete{$indice_a_prendre} = 1;
			$cpt = $cpt + 1;
		}
	}
}

close ARFF or warn $! ? "Error closing sort pipe: $!"
                   : "Exit status $? from sort";
open (ARFF,'<',$fichier_in) || die "Can't open file:$!\n";
open (OUT_APP, '>', $out_comptage) || die "Can't open file:$!\n";
open (OUT_INC, '>', $out_inconnu) || die "Can't open file:$!\n";

$indice_ligne = 0;
$flag_stop = 0;
my $cpt2 = 0;
while (<ARFF>) 
{
	my $l = $_;
	chomp($l);
	$l =~ s/^\s+|\s+$//g;

	if($flag_stop == 0)
	{
		print OUT_INC "$l\n";
	}

	if ($l =~ m/^(@)(data)/)
	{
		$flag_stop = 1;
	}
	$indice_ligne = $indice_ligne + 1;

	# sauvegarde de la ligne
	if($hash_indice_delete{$indice_ligne}==1)
	{

		if($l =~ m/(^[^@])(.*)(,)(.*$)/)
		{
			my $id = $4;
			$cpt2 = $cpt2 + 1;
			# print "id = $id\n";
			# $l =~ s/($id)$/?/;
			print OUT_INC "$l";
			print OUT_INC " % $id attendu $cpt2";
			print OUT_INC "\n";
		}
	}
	else
	{
		print OUT_APP "$l\n";
	}	
}